package com.fan.txl.widget.listener;

import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by os on 2017/4/4.
 * Title
 */

public abstract class OnRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout mSwipeRefreshLayout;
    public OnRefreshListener() {

    }

    public void setmSwipeRefreshLayout(@NonNull  SwipeRefreshLayout mSwipeRefreshLayout) {
        this.mSwipeRefreshLayout = mSwipeRefreshLayout;
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    public abstract void refreshUp(SwipeRefreshLayout swipeRefreshLayout);
    public abstract void refreshDown(SwipeRefreshLayout swipeRefreshLayout);


    @Override
    public void onRefresh() {
        if (mSwipeRefreshLayout!=null){
            boolean refresh= mSwipeRefreshLayout.isRefreshing();
            if (refresh){
                Observable.timer(5, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });
            }else{
                mSwipeRefreshLayout.setRefreshing(true);
                refreshDown(mSwipeRefreshLayout);
            }
        }
    }
}
